<?php

namespace App\Http\Controllers\applet_api;

use App\Http\Controllers\Controller;
use App\Models\Concern;
use App\Models\Interaction;
use App\Models\InteractionCollect;
use App\Models\InteractionComment;
use App\Models\InteractionCommentLike;
use App\Models\InteractionLike;
use Illuminate\Http\Request;

class InteractionController extends Controller
{

    public function index(Request $Request)
    {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $sql = Interaction::with('types')->with('user')->when($search, function ($query, $search) {
                    return $query->where('title','like', $search.'%');
                });
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('id','desc')->get();
        foreach ($data as $k => $v){
            $data[$k]->type_name = '';
            if($v->types){
                $data[$k]->type_name = $v->types->name;
            }
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total]);
    }


    public function info(Request $Request)
    {
        $data = Interaction::withCount('likes')->withCount('collections')->with('comments')->find($Request->id);
        $data->increment('click_um');
        //是否关注过
        $data->concern = false;
        $is = Concern::where('uid',$Request->uid)->where('concern_user',$data->uid)->first();
        if($is){
            $data->concern = true;
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function store(Request $Request)
    {
        $data = new Interaction();
        $data->uid = $Request->uid;
        $data->content = $Request->content;
        $data->img = $Request->img;
        $data->title = $Request->title;
        $data->type = $Request->type ?? 0;
        $data->click_um = 0;
        $data->likes_num = 0;
        $data->collect_num = 0;
        $data->reply_num = 0;
        $data->is_pass = 1;
        $data->is_search = 0;
        $data->is_merge = 0;
        $data->create_time = time();
        $data->update_time = time();
        $data->save();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function like(Request $Request)
    {
        $data = InteractionLike::where('interaction_id',$Request->id)->where('uid',$Request->uid)->first();
        if($data){
            $data->delete();
        }else{
            $data = new InteractionLike();
            $data->interaction_id = $Request->id;
            $data->uid = $Request->uid;
            $data->create_time = time();
            $data->update_time = time();
            $data->save();
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function collection(Request $Request)
    {
        $data = InteractionCollect::where('interaction_id',$Request->id)->where('uid',$Request->uid)->first();
        if($data){
            $data->delete();
        }else{
            $data = new InteractionCollect();
            $data->interaction_id = $Request->id;
            $data->uid = $Request->uid;
            $data->create_time = time();
            $data->update_time = time();
            $data->save();
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function comment(Request $Request)
    {
        $data = new InteractionComment();
        $data->user_id = $Request->uid;
        $data->content = $Request->content;
        $data->article_id = $Request->id;
        $data->to_uid = 0;
        $data->likes = 0;
        $data->review = 0;
        $data->r_id = 0;
        $data->create_time = time();
        $data->update_time = time();
        $data->save();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }


    public function commentLike(Request $Request)
    {
        $data = InteractionCommentLike::where('reply_id',$Request->id)->where('uid',$Request->uid)->first();
        if($data){
            $data->delete();
        }else{
            $data = new InteractionCommentLike();
            $data->reply_id = $Request->id;
            $data->uid = $Request->uid;
            $data->create_time = time();
            $data->update_time = time();
            $data->save();
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

}
